<?php
class BookModel extends RelationModel
{
	public $_link = array (
	
	 'group' => array(   
 					'mapping_type'=>BELONGS_TO, 
                    'class_name'=>'group', 
                    'foreign_key'=>'group_id', 
                    'mapping_name'=>'group',
                   ),
      'user_id' => array(   
 					'mapping_type'=>BELONGS_TO, 
                    'class_name'=>'user', 
                    'foreign_key'=>'user_id', 
                    'mapping_name'=>'user',
                   )       
	);
	
	public function getById($book_id) {
		$condition['id'] = $book_id;
		$book = $this->where($condition)->relation(true)->find();
		$label_dao = new LabelModel();
		$book['labels'] = $label_dao->getByBookId($book_id);
		return $book;
	}
	
	public function getNewestBooks($num) {
		$books = $this->relation(true)
			->order("create_time desc")->limit($num)->select();
		
		return $books;
	}
	
	public function getAll($record_num)
	{
		import("ORG.Util.Page"); //  导入分页类 
	 	$count = $this->count();
	 	$Page = new Page ( $count, $record_num );
	 	$show = $Page->show (); //  分页显示输出 
	 	$books = $this->relation(true)
			->order('create_time desc')
			->limit ( $Page->firstRow.','.$Page->listRows )->select();
		$result = array('data'=>$books,'page'=>$show);
	 	return $result;
	}
	
	public function getByGroupId($group_id) {
		$condition['group_id'] = $group_id;
		return $this->where($condition)->relation(true)->select();
	}
}
?>